using Autofac;
using EFGetStarted;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.OpenApi.Models;
using System;
using ZFY.AspNetCore;
using ZFY.DDD.Domain.Repositories;
using ZFY.EntityFrameworkCore;
using ZFY.UnitOfWork;


namespace BlogApi
{
    public class Startup
    {
        private readonly IWebHostEnvironment _hostEnvironment;

        public Startup(IConfiguration configuration, IWebHostEnvironment hostEnvironment)
        {
            Configuration = configuration;
            _hostEnvironment = hostEnvironment;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {

            services.AddControllers(x =>
            {

                x.Filters.Add<UOWActionFilter>();
                x.Filters.Add<ZFYResultFilter>();
            });
            services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
            services.AddScoped(typeof(IBaseRepository<>), typeof(EFRepository<,>));
            services.AddDbContext<BloggingContext>(option =>
            {

                if (_hostEnvironment.IsDevelopment())
                {
                    option.EnableSensitiveDataLogging();
                }

            });
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo { Title = "BlogApi", Version = "v1" });
            });
            services.AddHttpContextAccessor();
            services.AddHealthChecks();
      
            //services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            //StaticInfo.ServiceProvider = app.ApplicationServices;
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseSwagger();
                app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "BlogApi v1"));
            }

            app.UseRouting();


           // app.UseZFY();
          
            
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapHealthChecks("/health");
                endpoints.MapControllers();
            });
        }

        public void ConfigureContainer(ContainerBuilder containerBuilder)
        {
            containerBuilder.RegisterModule<ZFYAutofacModule>();
        }
    }
}
